'use strict';
const Service = require('egg').Service;
class ClazzService extends Service {
  // 增
  async create() {
    const name = this.ctx.request.body.name;
    try {
      await this.app.model.Clazz.create({
        name,
      });
      return this.ctx.response._success('添加成功');
    } catch (error) {
      return this.ctx.response._error('创建班级失败');
    }
  }
  // 删
  async destory() {
    const id = this.ctx.params.id;
    try {
      await this.app.model.Clazz.destory({
        where: {
          id,
        },
      });
      return this.ctx.response._success('成功删除');
    } catch (error) {
      return this.ctx.response._error('删除失败');
    }
  }
  // 改
  async update() {
    const id = this.ctx.params.id;
    const name = this.ctx.request.body.name;
    try {
      await this.app.model.Clazz.update({ name }, { where: { id } });
      return this.ctx.response._success();
    } catch (error) {
      return this.ctx.response._error('修改失败');
    }

  }
  // 查
  async showById() {
    try {
      const id = this.ctx.request.query.id;
      const data = await this.app.model.Clazz.findAll({
        where: {
          id,
        },
      });
      return this.ctx.response._success(data);
    } catch (error) {
      return this.ctx.response._error('没有找到此班级信息');
    }

  }
  async Allshow() {

    try {
      const list = await this.app.model.Clazz.findAll();
      return this.ctx.response._success(list);
    } catch (error) {
      return this.ctx.response._error(error.message);
    }
  }

}

module.exports = ClazzService;
